package com.motu.monstercity.server.game.logic;

import com.motu.monstercity.server.game.commondata.partner.*;
import com.motu.monstercity.server.game.commondata.skill.Skill;
import com.motu.monstercity.server.game.commondata.skill.SkillUpgradeType;
import com.motu.vertx.module.utility.toolset.Tool;
import io.vertx.core.Future;
import io.vertx.core.Promise;
import io.vertx.core.json.JsonArray;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.motu.vertx.module.utility.mysql.MysqlClient;
import com.motu.vertx.module.utility.mysql.MysqlTool;

import java.util.List;
import java.util.concurrent.ConcurrentHashMap;


public class PartnerBaseData {
    private static Logger logger = LoggerFactory.getLogger(PartnerBaseData.class);
    private static ConcurrentHashMap<Integer, PartnerMain> partnerMainMap = new ConcurrentHashMap<>(); // 干员伙伴表
    private static ConcurrentHashMap<Integer, PartnerBreakthrough> partnerBreakthroughMap = new ConcurrentHashMap<>(); // 干员突破配置表
    private static ConcurrentHashMap<Integer, PartnerLevelUp> partnerLevelUpMap = new ConcurrentHashMap<>(); // 干员升级配置表
    private static ConcurrentHashMap<Integer, WeaponMain> weaponMainMap = new ConcurrentHashMap<>(); // 武器配置表
    private static ConcurrentHashMap<Integer, WeaponAwake> weaponAwakeMap = new ConcurrentHashMap<>(); // 武器觉醒配置表
    private static ConcurrentHashMap<Integer, PartnerStar> partnerStarMap = new ConcurrentHashMap<>(); // 干员升星表
    private static ConcurrentHashMap<Integer, PartnerHalo> partnerHaloMap = new ConcurrentHashMap<>(); // 干员光环技能表



    // 公共数据初始化
    public static void init(List<Future> futures) {
        futures.add(initPartnerMain());
        futures.add(initPartnerBreakthrough());
        futures.add(initPartnerLevelUp());
        futures.add(initWeaponMain());
        futures.add(initWeaponAwake());
        futures.add(initPartnerStar());
        futures.add(initPartnerHalo());
    }

    /**
     * 初始化干员伙伴表
     */
    public static Future<Void> initPartnerMain() {
        String table = "partner_main";
        Promise promise = Promise.promise();
        String sql = "select * from " + table + ";";
        MysqlTool.queryJsonArrayFromDb(MysqlClient.getCommonPool(), sql, new JsonArray(), res -> {
            if (res.succeeded()) {
                try {
                    ConcurrentHashMap<Integer, PartnerMain> tmp = new ConcurrentHashMap<>();
                    JsonArray result = res.result();
                    logger.info("init " + table + " size:" + result.size());
                    for (int i = 0; i < result.size(); i++) {
                        PartnerMain entity = new PartnerMain(result.getJsonObject(i));
                        tmp.put(entity.getPartnerId(), entity);
                    }
                    partnerMainMap = tmp;
                    promise.complete();
                } catch (Exception e) {
                    logger.error("init " + table + " error exception:" + Tool.getException(e));
                    promise.fail(e);
                }
            } else {
                logger.error(res.cause().getMessage());
                promise.fail(res.cause());
            }
        });
        return promise.future();
    }

    public static PartnerMain getPartnerMain(int id) {
        PartnerMain result = partnerMainMap.get(id);
        if (result == null) {
            logger.error("getPartnerMain error key = " + id);
        }
        return result;
    }
    public static ConcurrentHashMap<Integer, PartnerMain> getPartnerMainMap() { return partnerMainMap; }

    /**
     * 初始化干员突破配置表
     */
    public static Future<Void> initPartnerBreakthrough() {
        String table = "partner_breakthrough";
        Promise promise = Promise.promise();
        String sql = "select * from " + table + ";";
        MysqlTool.queryJsonArrayFromDb(MysqlClient.getCommonPool(), sql, new JsonArray(), res -> {
            if (res.succeeded()) {
                try {
                    ConcurrentHashMap<Integer, PartnerBreakthrough> tmp = new ConcurrentHashMap<>();
                    JsonArray result = res.result();
                    logger.info("init " + table + " size:" + result.size());
                    for (int i = 0; i < result.size(); i++) {
                        PartnerBreakthrough entity = new PartnerBreakthrough(result.getJsonObject(i));
                        tmp.put(entity.getId(), entity);
                    }
                    partnerBreakthroughMap = tmp;
                    promise.complete();
                } catch (Exception e) {
                    logger.error("init " + table + " error exception:" + Tool.getException(e));
                    promise.fail(e);
                }
            } else {
                logger.error(res.cause().getMessage());
                promise.fail(res.cause());
            }
        });
        return promise.future();
    }

    public static PartnerBreakthrough getPartnerBreakthrough(int id) {
        PartnerBreakthrough result = partnerBreakthroughMap.get(id);
        if (result == null) {
            logger.error("getPartnerBreakthrough error key = " + id);
        }
        return result;
    }

    public static PartnerBreakthrough getPartnerBreakthroughByMaxLevel(int maxLevel) {
        for (PartnerBreakthrough partnerBreakthrough : getPartnerBreakthroughMap().values()) {
            if (maxLevel == partnerBreakthrough.getMaxLevel()) {
                return partnerBreakthrough;
            }
        }
        return null;
    }

    public static ConcurrentHashMap<Integer, PartnerBreakthrough> getPartnerBreakthroughMap() { return partnerBreakthroughMap; }


    /**
     * 初始化武器配置表
     */
    public static Future<Void> initWeaponMain() {
        String table = "weapon_main";
        Promise promise = Promise.promise();
        String sql = "select * from " + table + ";";
        MysqlTool.queryJsonArrayFromDb(MysqlClient.getCommonPool(), sql, new JsonArray(), res -> {
            if (res.succeeded()) {
                try {
                    ConcurrentHashMap<Integer, WeaponMain> tmp = new ConcurrentHashMap<>();
                    JsonArray result = res.result();
                    logger.info("init " + table + " size:" + result.size());
                    for (int i = 0; i < result.size(); i++) {
                        WeaponMain entity = new WeaponMain(result.getJsonObject(i));
                        tmp.put(entity.getWeaponId(), entity);
                    }
                    weaponMainMap = tmp;
                    promise.complete();
                } catch (Exception e) {
                    logger.error("init " + table + " error exception:" + Tool.getException(e));
                    promise.fail(e);
                }
            } else {
                logger.error(res.cause().getMessage());
                promise.fail(res.cause());
            }
        });
        return promise.future();
    }

    public static WeaponMain getWeaponMain(int id) {
        WeaponMain result = weaponMainMap.get(id);
        if (result == null) {
            logger.error("getWeaponMain error key = " + id);
        }
        return result;
    }
    public static ConcurrentHashMap<Integer, WeaponMain> getWeaponMainMap() { return weaponMainMap; }

    /**
     * 初始化干员升级配置表
     */
    public static Future<Void> initPartnerLevelUp() {
        String table = "partner_level_up";
        Promise promise = Promise.promise();
        String sql = "select * from " + table + ";";
        MysqlTool.queryJsonArrayFromDb(MysqlClient.getCommonPool(), sql, new JsonArray(), res -> {
            if (res.succeeded()) {
                try {
                    ConcurrentHashMap<Integer, PartnerLevelUp> tmp = new ConcurrentHashMap<>();
                    JsonArray result = res.result();
                    logger.info("init " + table + " size:" + result.size());
                    for (int i = 0; i < result.size(); i++) {
                        PartnerLevelUp entity = new PartnerLevelUp(result.getJsonObject(i));
                        tmp.put(entity.getLevel(), entity);
                    }
                    partnerLevelUpMap = tmp;
                    promise.complete();
                } catch (Exception e) {
                    logger.error("init " + table + " error exception:" + Tool.getException(e));
                    promise.fail(e);
                }
            } else {
                logger.error(res.cause().getMessage());
                promise.fail(res.cause());
            }
        });
        return promise.future();
    }

    public static PartnerLevelUp getPartnerLevelUp(int id) {
        PartnerLevelUp result = partnerLevelUpMap.get(id);
        if (result == null) {
            logger.error("getPartnerLevelUp error key = " + id);
        }
        return result;
    }
    public static ConcurrentHashMap<Integer, PartnerLevelUp> getPartnerLevelUpMap() { return partnerLevelUpMap; }

    // 初始的最大等级 partner_breakthrough 表中配置的最低档的max_level
    public static int getFirstMaxLevel() {
        int maxLevel = 0;
        for (PartnerBreakthrough partnerBreakthrough : getPartnerBreakthroughMap().values()) {
            if (maxLevel == 0 || maxLevel > partnerBreakthrough.getMaxLevel()) {
                maxLevel = partnerBreakthrough.getMaxLevel();
            }
        }
        return maxLevel;
    }

    // 获取下一档的伙伴突破配置
    public static PartnerBreakthrough getNextPartnerBreakthrough(int maxLevel) {
        int tmp = 0;
        PartnerBreakthrough result = null;
        for (PartnerBreakthrough partnerBreakthrough : getPartnerBreakthroughMap().values()) {
            if (partnerBreakthrough.getMaxLevel() <= maxLevel) {
                continue;
            }
            if ((tmp == 0 && partnerBreakthrough.getMaxLevel() > 0) || (tmp > 0 && partnerBreakthrough.getMaxLevel() < tmp)) {
                tmp = partnerBreakthrough.getMaxLevel();
                result = partnerBreakthrough;
            }
        }
        return result;
    }

    /**
     * 初始化武器觉醒配置表
     */
    public static Future<Void> initWeaponAwake() {
        String table = "weapon_awake";
        Promise promise = Promise.promise();
        String sql = "select * from " + table + ";";
        MysqlTool.queryJsonArrayFromDb(MysqlClient.getCommonPool(), sql, new JsonArray(), res -> {
            if (res.succeeded()) {
                try {
                    ConcurrentHashMap<Integer, WeaponAwake> tmp = new ConcurrentHashMap<>();
                    JsonArray result = res.result();
                    logger.info("init " + table + " size:" + result.size());
                    for (int i = 0; i < result.size(); i++) {
                        WeaponAwake entity = new WeaponAwake(result.getJsonObject(i));
                        tmp.put(entity.getAwakeNum(), entity);
                    }
                    weaponAwakeMap = tmp;
                    promise.complete();
                } catch (Exception e) {
                    logger.error("init " + table + " error exception:" + Tool.getException(e));
                    promise.fail(e);
                }
            } else {
                logger.error(res.cause().getMessage());
                promise.fail(res.cause());
            }
        });
        return promise.future();
    }

    public static WeaponAwake getWeaponAwake(int awakeNum) {
        WeaponAwake result = weaponAwakeMap.get(awakeNum);
        if (result == null) {
            logger.error("getWeaponAwake error key = " + awakeNum);
        }
        return result;
    }

    /**
     * 初始化干员升星表
     */
    public static Future<Void> initPartnerStar() {
        String table = "partner_star";
        Promise promise = Promise.promise();
        String sql = "select * from " + table + ";";
        MysqlTool.queryJsonArrayFromDb(MysqlClient.getCommonPool(), sql, new JsonArray(), res -> {
            if (res.succeeded()) {
                try {
                    ConcurrentHashMap<Integer, PartnerStar> tmp = new ConcurrentHashMap<>();
                    JsonArray result = res.result();
                    logger.info("init " + table + " size:" + result.size());
                    for (int i = 0; i < result.size(); i++) {
                        PartnerStar entity = new PartnerStar(result.getJsonObject(i));
                        tmp.put(entity.getId(), entity);
                    }
                    partnerStarMap = tmp;
                    promise.complete();
                } catch (Exception e) {
                    logger.error("init " + table + " error exception:" + Tool.getException(e));
                    promise.fail(e);
                }
            } else {
                logger.error(res.cause().getMessage());
                promise.fail(res.cause());
            }
        });
        return promise.future();
    }

    public static PartnerStar getPartnerStar(int stage, int quality) {
        for (PartnerStar partnerStar : getPartnerStarMap().values()) {
            if (partnerStar.getStage() == stage && partnerStar.getQuality() == quality) {
                return partnerStar;
            }
        }
        return null;
    }
    public static ConcurrentHashMap<Integer, PartnerStar> getPartnerStarMap() { return partnerStarMap; }

    /**
     * 初始化干员光环技能表
     */
    public static Future<Void> initPartnerHalo() {
        String table = "partner_halo";
        Promise promise = Promise.promise();
        String sql = "select * from " + table + ";";
        MysqlTool.queryJsonArrayFromDb(MysqlClient.getCommonPool(), sql, new JsonArray(), res -> {
            if (res.succeeded()) {
                try {
                    ConcurrentHashMap<Integer, PartnerHalo> tmp = new ConcurrentHashMap<>();
                    JsonArray result = res.result();
                    logger.info("init " + table + " size:" + result.size());
                    for (int i = 0; i < result.size(); i++) {
                        PartnerHalo entity = new PartnerHalo(result.getJsonObject(i));
                        tmp.put(entity.getHaloSkill(), entity);
                    }
                    partnerHaloMap = tmp;
                    promise.complete();
                } catch (Exception e) {
                    logger.error("init " + table + " error exception:" + Tool.getException(e));
                    promise.fail(e);
                }
            } else {
                logger.error(res.cause().getMessage());
                promise.fail(res.cause());
            }
        });
        return promise.future();
    }

    public static PartnerHalo getPartnerHalo(int haloSkill) {
        PartnerHalo result = partnerHaloMap.get(haloSkill);
        if (result == null) {
            logger.error("getPartnerHalo error key = " + haloSkill);
        }
        return result;
    }
    public static ConcurrentHashMap<Integer, PartnerHalo> getPartnerHaloMap() { return partnerHaloMap; }

    // 获取干员光环技能的id
    public static PartnerHalo getPartnerHaloByPartnerId(int partnerId) {
        for (PartnerHalo partnerHalo : getPartnerHaloMap().values()) {
            int[] partnerIds = partnerHalo.takePartnerIs();
            if (partnerIds != null && Tool.isInList(partnerIds, partnerId)) {
                return partnerHalo;
            }
        }
        return null;
    }
}
